﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Xml.Linq;
using YLPublicService.TransferModel;
using System.Configuration;

namespace YLClient.Config
{
    public class ClientConfig
    {
        static void loadConfig()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            WorkItemSaveDir = config.AppSettings.Settings["WorkItemSaveDir"].Value;
            AutoLoginName = config.AppSettings.Settings["AutoLoginName"].Value;
            AutoLoginPwd = config.AppSettings.Settings["AutoLoginPwd"].Value;
        }
        static ClientConfig()
        {
            loadConfig();
        }
        public static string WorkItemSaveDir
        { get; set; }
        public static string AutoLoginName
        { get; set; }
        public static string AutoLoginPwd
        { get; set; }
        /// <summary>
        /// 修改服务的地址用
        /// </summary>
        public static string ServiceAddress
        {
            get
            {
                return getServiceAddress();
            }
            set
            {
                setServiceAddress(value);
            }
        }
        #region 服务地址帮助方法
        private static string configPath = string.Empty;
        private static string ConfigPath
        {
            get
            {
                if (configPath == string.Empty)
                {
                    var file = Environment.CurrentDirectory;
                    var dir = Common.getDir(file);
                    configPath = dir + "\\" + System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe.config";
                }
                return configPath;
            }
        }
        private static string getServiceAddress()
        {
            var configDoc = XDocument.Load(ConfigPath);
            var address =
                configDoc.Element("configuration")
                .Element("system.serviceModel")
                .Element("client")
                .Element("endpoint")
                .Attribute("address");
            return address.Value;
        }
        private static void setServiceAddress(string value)
        {
            var configDoc = XDocument.Load(ConfigPath);
            var address =
                configDoc.Element("configuration")
                .Element("system.serviceModel")
                .Element("client")
                .Element("endpoint")
                .Attribute("address");
            address.Value = value;
            configDoc.Save(ConfigPath);
        }
        #endregion

        public static string GetRemoteSaveDir(WorkItem wi)
        {
            return Common.toDirString(wi.DBTime) + "\\" + wi.TemplateName;
        }

        public static void Save()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings["WorkItemSaveDir"].Value = WorkItemSaveDir;
            config.AppSettings.Settings["AutoLoginName"].Value = AutoLoginName;
            config.AppSettings.Settings["AutoLoginPwd"].Value = AutoLoginPwd;
            config.Save();
            loadConfig();
        }
    }
}
